package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.AddressBook;
import com.itheima.reggie.sercice.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     *
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping
    public R saveAddress(@RequestBody AddressBook addressBook, HttpSession session) {
        Long id = (Long) session.getAttribute("user");
        addressBook.setUserId(id);
        addressBookService.save(addressBook);

        /*addressBook.setId(BaseContext.getId());
        addressBookService.save(addressBook);*/

        return R.success("添加成功");
    }

    /**
     * 修改默认地址
     *
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping("/default")
    public R updateDefaultAddress(@RequestBody AddressBook addressBook, HttpSession session) {
        /*User user = (User) session.getAttribute("user");
        Long id = user.getId();*/
        Long id = (Long) session.getAttribute("user");
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AddressBook::getIsDefault, 0).eq(AddressBook::getUserId, id);
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getAddress(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        }
        return R.error("查询失败");

    }

    /**
     * 查询指定用户的全部地址
     *
     * @return
     */
    @GetMapping("/list")
    public R getList() {
        //获取指定用户的id
        Long id = BaseContext.getId();
        //构造条件构造器
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        //确认用户id
        wrapper.eq(AddressBook::getUserId, id);
        //调用方法 查询地址集合
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R getDefaultAddr() {
        //获取用户的id
        Long id = BaseContext.getId();
        //构造条件构造器
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        //判断条件  id相同 默认地址就设为1
        wrapper.eq(AddressBook::getUserId, id).eq(AddressBook::getIsDefault, 1);
        //调用方法  只查询符合条件的一个
        AddressBook addressBook = addressBookService.getOne(wrapper);
        return R.success(addressBook);

    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R updateAddr(@RequestBody AddressBook addressBook) {

        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }

    /**
     * 删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delAddr(@RequestParam List<Long> ids) {
        addressBookService.removeByIds(ids);

        return R.success("删除成功");

    }

}
